
Skt.GroupsManager = L.Class.extend({
	
	includes: L.Mixin.Events,
	
	events: ['groupUpdate','groupsUpdated','invitationUpdate','groupRoomListUpdate','roomModeratorUpdate'],

	initialize: function(sketchpad) {
		this._sketchpad = sketchpad;
		this._noteGroups = [];
		this._workGroups = [];
		this._couple(this._sketchpad.getOptions().coupling);
	},
	
	addWorkGroup: function(id, name, ownerId) {
		var self = this;
		if ( !self.groupExists(id) )
			return self.addGroup(name, Skt.GroupType.WORK, ownerId);
	},
	
	addNoteGroup: function(id, name, ownerId) {
		var self = this;
		if ( !self.groupExists(id) )
			return self.addGroup(name, Skt.GroupType.NOTES, ownerId);
	},
	
	addGroup: function(id, name, type, ownerId) {
		var self = this;
		var group = new Skt.Group(id, name, type, ownerId, self);
		
		if ( type == Skt.GroupType.NOTES )
			self._noteGroups.push(group);
		else
			self._workGroups.push(group);
		
		group.on('update', self._groupUpdate, self);
		group.on('roomListUpdate', self._groupRoomListUpdate, self);
		group.on('roomModeratorUpdate', self._roomModeratorUpdate, self);
		group.on('roomObjectAction', self._roomObjectAction, self);
		
		self.fire('groupsUpdated', {
			groupManager: self,
			action: 'addGroup',
			group: group
		});
		
		return group;
	},
	
	issueInvitation: function(groupId, inviterId, invitedId) {
		var self = this;
		self.fire('invitationUpdate', {
			type: Skt.InvitationType.ISSUED,
			groupId: groupId,
			inviterId: inviterId,
			invitedId: invitedId,
			groupsManager: self
		});
	},
	
	dismissInvitation: function(groupId, inviterId, invitedId) {
		var self = this;
		self.fire('invitationUpdate', {
			type: Skt.InvitationType.DISMISSED,
			groupId: groupId,
			inviterId: inviterId,
			invitedId: invitedId,
			groupsManager: self
		});
	},
	
	findGroup: function(id) {
		var self = this;
		for ( var m = 0; m < self._noteGroups.length; ++m )
			if ( self._noteGroups[m].getId() == id )
				return self._noteGroups[m];
		for ( var m = 0; m < self._workGroups.length; ++m )
			if ( self._workGroups[m].getId() == id )
				return self._workGroups[m];
		return null;
	},
	
	groupExists: function(id) {
		return this.findGroup(id) != null;
	},
	
	getNoteGroups: function() {
		return this._noteGroups;
	},
	
	getWorkGroups: function() {
		return this._workGroups;
	},
	
	getAllGroups: function() {
		return this._noteGroups.concat(this._workGroups);
	},
	
	getSketchpad: function() {
		return this._sketchpad;
	},
	
	_groupUpdate: function(params) {
		var self = Skt.GroupsManager.findSelf();
		self.fire('groupUpdate', params); 
	},
	_groupRoomListUpdate: function(params) { 
		var self = Skt.GroupsManager.findSelf();
		self.fire('groupRoomListUpdate', params); 
	},
	_roomModeratorUpdate: function(params) { 
		var self = Skt.GroupsManager.findSelf();
		self.fire('roomModeratorUpdate', params); 
	},
	
	_couple: function( couplingOptions ) {
		if ( couplingOptions.enabled ) {
			couplingOptions.activityAdapter.coupleObject(
				"groupsmanager", 
				this, 
				{
					messageType: "EVENT",
					explicitMapping: ["issueInvitation", "dismissInvitation", "addGroup"]
				}
			);
		}
	}

});

Skt.GroupsManager.findSelf = function() {
	return Skt.sketchpad.getGroupsManager();
};

